//
//  UserDetailViewController.m
//  Gruphoto
//
//  Created by Thao Nguyen Huy on 1/14/13.
//  Copyright (c) 2013 CNC Software. All rights reserved.
//

#import "UserDetailViewController.h"
#import "EventLargeCell.h"
#import "EventPhotoCell.h"
#import "EventJoinCell.h"
#import "EventDetailViewController.h"
#import "PhotoDetailViewController.h"

@interface UserDetailViewController ()

@end

@implementation UserDetailViewController

@synthesize userDetailLabels, deviders, detailButtons;
@synthesize eventsTableView, photosTableView, followersTableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        UIImageView *titleImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"title_gruphoto.png"]];
        self.navigationItem.titleView = titleImageView;
        [titleImageView release];
        
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [backButton setBounds:CGRectMake(0, 0, 51, 30)];
        [backButton setBackgroundImage:[UIImage imageNamed:@"button_back.png"] forState:UIControlStateNormal];
        [backButton addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
        self.navigationItem.leftBarButtonItem = backButtonItem;
        [backButtonItem release];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"background_navigation_bar.png"]
                                                      forBarMetrics:UIBarMetricsDefault];
    }
    self.view.backgroundColor = [UIColor colorWithRed:226/255.0 green:231/255.0 blue:237/255.0 alpha:1.0];
    for (UIView *devider in deviders) {
        if (devider.tag == 1) {
            devider.backgroundColor = [UIColor colorWithRed:129/255.0 green:146/255.0 blue:159/255.0 alpha:1.0];
        }
        else {
            devider.backgroundColor = [UIColor colorWithRed:190/255.0 green:191/255.0 blue:193/255.0 alpha:1.0];
        }
    }
    for (UILabel *label in userDetailLabels) {
        if (label.tag == 0) {
            label.textColor = [UIColor colorWithRed:71/255.0 green:138/255.0 blue:197/255.0 alpha:1.0];
        }
        else {
            label.textColor = [UIColor colorWithRed:107/255.0 green:120/255.0 blue:130/255.0 alpha:1.0];
        }
    }
    
    detailType = UserDetailTypeEvents;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.userDetailLabels = nil;
    self.deviders = nil;
    self.detailButtons = nil;
    self.eventsTableView = nil;
    self.photosTableView = nil;
    self.followersTableView = nil;
}

- (void)dealloc {
    [userDetailLabels release];
    [deviders release];
    [detailButtons release];
    [eventsTableView release];
    [photosTableView release];
    [followersTableView release];
    [super dealloc];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Action methods
- (IBAction)backButtonPressed:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)detailButtonPressed:(id)sender {
    detailType = [sender tag];
    for (UIButton *button in detailButtons) {
        if (button.tag == detailType) {
            button.enabled = NO;
        }
        else {
            button.enabled = YES;
        }
    }
    switch (detailType) {
        case UserDetailTypeEvents:
            photosTableView.hidden = YES;
            followersTableView.hidden = YES;
            eventsTableView.hidden = NO;
            [eventsTableView reloadData];
            break;
        case UserDetailTypePhotos:
            eventsTableView.hidden = YES;
            followersTableView.hidden = YES;
            photosTableView.hidden = NO;
            [photosTableView reloadData];
            break;
        case UserDetailTypeFollowers:
            eventsTableView.hidden = YES;
            photosTableView.hidden = YES;
            followersTableView.hidden = NO;
            [followersTableView reloadData];
            break;
        default:
            break;
    }
}

- (IBAction)followButtonPressed:(id)sender {
    UIButton *button = (UIButton *)sender;
    button.selected = !button.selected;
    NSString *imageName;
    if (button.selected) {
        imageName = @"button_following.png";
    }
    else {
        imageName = @"button_follow.png";
    }
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *LargeCellId = @"EventLargeCell";
    static NSString *EventPhotoCellId = @"EventPhotoCell";
    static NSString *EventUserCellId = @"EventJoinCell";
    
    if (tableView == eventsTableView) {
        EventLargeCell *cell = (EventLargeCell *)[tableView dequeueReusableCellWithIdentifier:LargeCellId];
        if (cell == nil) {
            cell = [EventLargeCell cellFromNib];
        }
        return cell;
    }
    else
    if (tableView == followersTableView) {
        EventJoinCell *cell = (EventJoinCell *)[tableView dequeueReusableCellWithIdentifier:EventUserCellId];
        if (cell == nil) {
            cell = [EventJoinCell cellFromNib];
            cell.distanceLabel.hidden = YES;
            cell.eventTitleLabel.text = @"Daniel Kussin";
            cell.detailLabel.text = [NSString stringWithFormat:@"Join %d hours ago", indexPath.row + 1];
        }
        return cell;
    }
    else {
        EventPhotoCell *cell = (EventPhotoCell *)[tableView dequeueReusableCellWithIdentifier:EventPhotoCellId];
        if (cell == nil) {
            cell = [EventPhotoCell cellFromNib];
        }
        for (UIView *holder in cell.placeHolders) {
            CGRect frame = CGRectMake(0, 0, kViewWidth, kViewHeight);
            EventPhotoView *view = [EventPhotoView viewFromNib];
            view.delegate = self;
            view.frame = frame;
            view.footerView.hidden = YES;
            [holder addSubview:view];
        }
        return cell;
    }
}

#pragma mark - EventPhotoView
- (void)didSelectPhotoAtIndex:(NSInteger)index {
    PhotoDetailViewController *photoDetailViewController = [[PhotoDetailViewController alloc] initWithNibName:@"PhotoDetailViewController" bundle:nil];
    [self.navigationController pushViewController:photoDetailViewController animated:YES];
    [photoDetailViewController release];
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == eventsTableView) {
        EventDetailViewController *eventDetailViewController = [[EventDetailViewController alloc] initWithNibName:@"EventDetailViewController" bundle:nil];
        eventDetailViewController.hidesBottomBarWhenPushed = YES;
        eventDetailViewController.detailMode = EventDetailModeUserDetail;
        [self.navigationController pushViewController:eventDetailViewController animated:YES];
        [eventDetailViewController release];
    }
    else if (tableView == followersTableView) {
        UserDetailViewController *userDetailViewController = [[UserDetailViewController alloc] initWithNibName:@"UserDetailViewController" bundle:nil];
        [self.navigationController pushViewController:userDetailViewController animated:YES];
        [userDetailViewController release];
    }
}


@end
